Mockito এবং MockWebServer ব্যবহার করে API Testing

Java Technologies - স্প্রিং বুট ক্লায়েন্ট (Spring Boot Client) - Testing Spring Boot Clients
155

Spring Boot ক্লায়েন্টে (Spring Boot Client) Mockito এবং MockWebServer ব্যবহার করে API টেস্টিং একটি কার্যকর পদ্ধতি। এই পদ্ধতিতে, MockWebServer ব্যবহার করে একটি কৃত্রিম সার্ভার তৈরি করা হয়, যা API রেসপন্স সিমুলেট করতে পারে। Mockito ব্যবহার করে সার্ভিস বা ডিপেন্ডেন্সি গুলো মক করা যায়।


১. প্রয়োজনীয় ডিপেন্ডেন্সি

Maven ডিপেন্ডেন্সি (pom.xml):

<dependencies>
    <!-- Spring Boot Web -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-webflux</artifactId>
    </dependency>

    <!-- JUnit and Mockito -->
    <dependency>
        <groupId>org.mockito</groupId>
        <artifactId>mockito-core</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.mockito</groupId>
        <artifactId>mockito-junit-jupiter</artifactId>
        <scope>test</scope>
    </dependency>

    <!-- MockWebServer -->
    <dependency>
        <groupId>com.squareup.okhttp3</groupId>
        <artifactId>mockwebserver</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

২. API Service

উদাহরণ সার্ভিস ক্লাস:

import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.client.WebClient;

@Service
public class ApiService {

    private final WebClient webClient;

    public ApiService(WebClient webClient) {
        this.webClient = webClient;
    }

    public String fetchData(String endpoint) {
        return webClient.get()
                .uri(endpoint)
                .retrieve()
                .bodyToMono(String.class)
                .block();
    }
}

৩. টেস্ট লিখার পদ্ধতি

Test Class:

import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.MockWebServer;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.MockitoAnnotations;
import org.springframework.web.reactive.function.client.WebClient;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class ApiServiceTest {

    private MockWebServer mockWebServer;

    @InjectMocks
    private ApiService apiService;

    @BeforeEach
    void setUp() {
        MockitoAnnotations.openMocks(this);
        mockWebServer = new MockWebServer();
        mockWebServer.start();
        apiService = new ApiService(WebClient.builder().baseUrl(mockWebServer.url("/").toString()).build());
    }

    @AfterEach
    void tearDown() throws Exception {
        mockWebServer.shutdown();
    }

    @Test
    void testFetchData() {
        // Mock Response
        String mockResponseBody = "{\"message\": \"Hello, World!\"}";
        mockWebServer.enqueue(new MockResponse()
                .setBody(mockResponseBody)
                .addHeader("Content-Type", "application/json"));

        // Call the method
        String response = apiService.fetchData("/test");

        // Assertions
        assertEquals(mockResponseBody, response);
    }
}

৪. কীভাবে কাজ করে

  1. MockWebServer:
    • এটি একটি কৃত্রিম HTTP সার্ভার তৈরি করে।
    • রিকোয়েস্টের জন্য প্রস্তুত করা MockResponse প্রদান করে।
  2. WebClient Configuration:
    • MockWebServer.url("/") ব্যবহার করে WebClient-এর বেস URL সেট করা হয়।
  3. Mockito:
    • সার্ভিস বা ডিপেন্ডেন্সিগুলি মক করে টেস্টিং সহজ করা যায়।
  4. JUnit Assertions:
    • টেস্টের আউটপুট যাচাই করতে JUnit assertions ব্যবহার করা হয়।

৫. আরো উদাহরণ

যদি API তে নির্দিষ্ট স্ট্যাটাস কোডের রেসপন্স পরীক্ষা করতে চান:

@Test
void testFetchDataWithErrorResponse() {
    // Mock Response with Error
    mockWebServer.enqueue(new MockResponse()
            .setResponseCode(404)
            .setBody("{\"error\": \"Not Found\"}"));

    // Call the method
    String response = apiService.fetchData("/not-found");

    // Assertions
    assertEquals(null, response); // WebClient null রিটার্ন করতে পারে।
}

৬. গুরুত্বপূর্ণ টিপস

  1. কনকারেন্ট টেস্টিং:
    • MockWebServer সমান্তরাল রিকোয়েস্ট হ্যান্ডল করতে পারে।
  2. Custom Headers টেস্ট:
    • MockWebServer থেকে রিকোয়েস্টের হেডার যাচাই করা যায়:

      RecordedRequest recordedRequest = mockWebServer.takeRequest();
      assertEquals("Bearer my-token", recordedRequest.getHeader("Authorization"));
      
  3. Timeout Handling:
    • MockResponse এ কৃত্রিম delay যোগ করে টাইমআউট টেস্ট করতে পারেন:

      mockWebServer.enqueue(new MockResponse()
              .setBody("Delayed Response")
              .setBodyDelay(5, TimeUnit.SECONDS));
      

উপসংহার

Mockito এবং MockWebServer ব্যবহার করে Spring Boot ক্লায়েন্টের জন্য API টেস্টিং খুবই কার্যকর। এই পদ্ধতিতে:

  • MockWebServer কৃত্রিম সার্ভার তৈরি করে।
  • Mockito প্রয়োজনীয় ডিপেন্ডেন্সি মক করে।
  • JUnit assertions ব্যবহার করে রেসপন্স যাচাই করা যায়।

যদি আপনি নির্দিষ্ট কোনো কনফিগারেশন বা টেস্টিং ইস্যু নিয়ে সাহায্য চান, জানান! 😊

Content added By
Promotion
NEW SATT AI এখন আপনাকে সাহায্য করতে পারে।

Are you sure to start over?

Loading...